package com.xjust.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjust.Mapper.CheckRequestMapper;
import com.xjust.Mapper.EmployeeMapper;
import com.xjust.Mapper.RegisterMapper;
import com.xjust.pojo.ResultBean;
import com.xjust.service.CheckRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CheckRequestServiceImpl implements CheckRequestService {
    @Autowired
   private CheckRequestMapper checkRequestMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    //获取已完成患者数量
    @Override
    public Integer getFinishPatientList() {

        return checkRequestMapper.selectFinishPatientList();
    }
    //获取等待患者数量
    @Override
    public Integer getWaitPatientList() {
        return checkRequestMapper.selectWaitPatientList();
    }
    //获取患者列表
    @Override
    public ResultBean getPatientList(Integer page, Integer pageSize, String realName, String caseNumber) {
        List<Map<String, Object>> list = checkRequestMapper.selectPatientList();
        List<Integer> idList = new ArrayList<>();
        if (list.size() != 0) {
            for (Map<String, Object> regist : list) {
                if (regist.get("register_id") != null) {
                    idList.add((Integer) regist.get("register_id"));
                }
            }
            Integer[] registIdList = idList.toArray(new Integer[idList.size()]);
            PageHelper.startPage(page, pageSize);
            Page<Map<String, Object>> pageInfo = registerMapper.selectRegisterById(registIdList, realName, caseNumber);
            ResultBean pageBean = new ResultBean();
            pageBean.setRows(pageInfo.getResult());
            pageBean.setCount(pageInfo.getTotal());
            return pageBean;
        }else{
            return null;
        }
    }
    //提交患者检查信息
    @Override
    public void submitCheckRequest(Integer employeeId,Integer registId,Integer medicalTechnology) {
        String checkState = "执行完成";
        checkRequestMapper.updataCheckRequest(employeeId,registId,medicalTechnology,checkState);
    }
    //提交患者检查结果
    @Override
    public void submitToCheckRequest(String textarea, Integer registId, Integer medicalTechnology,Integer employeeId) {
        LocalDateTime checkTime = LocalDateTime.now();
        checkRequestMapper.updataToCheckRequest(textarea,registId,medicalTechnology,employeeId,checkTime);
    }

    @Override
    public Map<String,Object> selectCheck(Integer checkId) {
        Map<String,Object> list = checkRequestMapper.selectCheck(checkId);
       Integer id = (Integer) list.get("check_employee_id");
       String name = employeeMapper.selectName(id);
       list.put("check_doctor_name",name);
        return list;
    }
}
